<template>
  <div>
    <!-- 按钮 -->
    <el-row style="margin: 15px">
      <el-col :span="8">
        <el-button type="primary" size="small" @click="addClick"
          >新增</el-button
        >
      </el-col>
      <el-col :span="16">
        <el-input
          placeholder="请输入内容"
          v-model="input"
          class="input-with-select"
        >
          <el-button
            slot="append"
            @click="seachClick"
            icon="el-icon-search"
          ></el-button>
        </el-input>
      </el-col>
    </el-row>
    <!-- 表格 -->
    <el-table :data="tableData" border style="width: 98%; margin: 0 auto">
      <el-table-column type="index" label="#" width="100"> </el-table-column>
      <el-table-column prop="title" label="标题" width="400">
        <template slot-scope="scope">
          <el-link type="primary">{{ scope.row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="type" label="分类" width="120"> </el-table-column>
      <el-table-column prop="author" label="作者" width="120">
      </el-table-column>
      <el-table-column prop="time" label="用时" width="120"> </el-table-column>
      <el-table-column prop="date" label="发布时间" width="200">
      </el-table-column>
      <el-table-column label="操作" width="250">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row.id)" type="text" size="small"
            >删除</el-button
          >
          <el-button type="text" size="small" @click="updataClick(scope.row)"
            >修改</el-button
          >
          <el-button type="text" size="small" @click="getPdf()">导出</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[1, 3, 5, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 抽屉 -->
    <el-drawer
      size="70%"
      title="我是标题"
      :visible.sync="drawer"
      :with-header="false"
    >
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        style="padding: 10px"
      >
        <el-form-item label="标题">
          <el-input style="width: 500px" v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="分类">
          <el-select v-model="form.type" placeholder="请选择类别">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用时">
          <el-select v-model="form.time" placeholder="请选择时间">
            <el-option label="一分钟" value="1"></el-option>
            <el-option label="区域二" value="2"></el-option>
            <el-option label="区域二" value="3"></el-option>
            <el-option label="区域二" value="5"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="内容">
          <mavonEditor v-model="form.content" />
        </el-form-item>
      </el-form>

      <el-button style="margin: 20px 0 0 60px" type="primary" @click="onSubmit"
        >立即创建</el-button
      >
      <el-button>取消</el-button>
    </el-drawer>
  </div>
</template>

<script>
import { mavonEditor } from "mavon-editor";
import "mavon-editor/dist/css/index.css";
import { mapState, mapActions, mapMutations } from "vuex";
export default {
  data() {
    return {
      input: "",
      drawer: false,
      form: { author: "situ" },
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    ...mapActions("msbd", ["getData", "addData", "deleData", "updataData"]),
    ...mapMutations("msbd", ["GET_CURRENTPAGE", "GET_PAGESIZE", "GET_TOTAL"]),
    //新增
    addClick() {
      this.drawer = true;
    },
    //立即创建
    onSubmit() {
      if (this.form.id) {
        this.updataData({ ...this.form });
      } else {
        this.addData({ ...this.form, cid: 1 });
      }
      this.drawer = false;
      this.form = {};
    },
    //修改
    updataClick(row) {
      this.drawer = true;
      this.form = JSON.parse(JSON.stringify(row));
    },
    getPdf() {
      console.log(123);
    },
    //搜索
    seachClick() {
      this.getData(this.input);
    },
    //删除
    handleClick(id) {
      this.deleData(id);
    },
    //每页几条
    handleSizeChange(val) {
      this.GET_PAGESIZE(val);
      this.getData();
    },
    //当前第几页
    handleCurrentChange(val) {
      this.GET_CURRENTPAGE(val);
      this.getData();
    },
  },
  computed: {
    ...mapState("msbd", ["tableData", "currentPage", "pageSize", "total"]),
  },
  components: {
    mavonEditor,
  },
};
</script>

<style scoped>
</style>